/**
 * Project Name:mauto-web
 * File Name:PJManagerController.java
 * Package Name:com.inftech.mauto.jy.web
 * Date:2017年6月27日下午12:38:50
 * Copyright (c) 2017, inf-technology Co., Ltd. All Rights Reserved.
 *
*/

package com.inftech.mauto.jy.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.JsonObject;
import com.inftech.mauto.common.utils.CommonUtils;
import com.inftech.mauto.jy.entity.JyItemPjlb;
import com.inftech.mauto.jy.entity.JyItempjpp;
import com.inftech.mauto.jy.service.JyItemPjService;
import com.inftech.mauto.jy.service.JyItemPjlbService;
import com.inftech.mauto.jy.service.JyItempjppService;
import com.inftech.mauto.sys.service.CarBasicService;
import com.mingsoft.base.action.BaseAction;
import com.mingsoft.basic.constant.e.SessionConstEnum;
import com.mingsoft.basic.entity.ManagerSessionEntity;

import net.mingsoft.basic.util.BasicUtil;
import net.sf.json.JSONObject;

/**
 * ClassName:PJManagerController <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Date: 2017年6月27日 下午12:38:50 <br/>
 * 
 * @author Administrator
 * @version
 * @since JDK 1.7
 */
@Controller
@RequestMapping("/${managerPath}/pj")
@Transactional(rollbackFor = Exception.class)
public class PJManagerAction extends BaseAction {

	@Resource(name = "jyItempjppServiceImpl")
	private JyItempjppService jyItempjppServiceImpl;
	@Resource
	private JyItemPjlbService jyItemPjlbServiceImpl;
	@Resource(name = "jyItemPjServiceImpl")
	private JyItemPjService jyItemPjServiceImpl;
	@Resource(name = "carBasicService")
	private CarBasicService carBasicServiceImpl;

	/**
	 * 
	 * getPjList:(配件管理). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping("/pjList.do")
	public String getPjList(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
		List<JyItemPjlb> pJlist = jyItemPjlbServiceImpl.getPJLB();
		List<JyItemPjlb> pJlistXZ = new ArrayList<JyItemPjlb>();
		pJlistXZ.addAll(pJlist);
		request.setAttribute("pJlistXZ", pJlistXZ);
		// List<CarModel> modelList = carBasicServiceImpl.getCnName();
		// request.setAttribute("modelList", modelList);
		JyItemPjlb element = new JyItemPjlb();
		element.setId(0);
		element.setPjlbName("全部");
		pJlist.add(0, element);
		List<JyItempjpp> pplist = jyItempjppServiceImpl.getPjppName();
		BasicUtil.startPage();
		request.setAttribute("pJlist", pJlist);
		request.setAttribute("pplist", pplist);
		Enumeration<String> enumeration = request.getParameterNames();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		String name = "";
		String value = "";
		while (enumeration.hasMoreElements()) {
			name = enumeration.nextElement();
			value = request.getParameter(name);
			paramMap.put(name, value);
		}
		// 配件查询信息
		List<Map<String, Object>> list = jyItemPjServiceImpl.getPJXX(paramMap);
		// for (Map<String, Object> map : list) {
		// map.put("hml", map.get("hml").toString().substring(0,
		// map.get("hml").toString().length()));
		// }
		request.setAttribute("list", list);
		BasicUtil.endPage(list);
		request.setAttribute("pageNo",
				CommonUtils.isBlank(request.getParameter("pageNo")) ? 1 : request.getParameter("pageNo"));
		return "/WEB-INF/manager/mauto/jy/pj";
	}

	/**
	 * 
	 * save:(保存配件). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 * @throws IllegalStateException
	 * @since JDK 1.7
	 */
	@RequestMapping({ "save.do" })
	public String save(HttpServletRequest request, HttpServletResponse response, Map<String, Object> param)
			throws IllegalStateException, IOException {
		// ShiroHttpServletRequest shiroRequest = (ShiroHttpServletRequest)
		// request;
		// CommonsMultipartResolver commonsMultipartResolver = new
		// CommonsMultipartResolver();
		// MultipartHttpServletRequest multipartRequest =
		// commonsMultipartResolver.resolveMultipart((HttpServletRequest)
		// shiroRequest.getRequest());
		Enumeration<String> enumeration = request.getParameterNames();
		String value = "";
		String name = "";
		while (enumeration.hasMoreElements()) {
			name = enumeration.nextElement();
			value = request.getParameter(name);
			param.put(name, value);
		}
		// MultipartFile file = multipartRequest.getFile("pjpic");
		// file.getName();
		// System.currentTimeMillis();
		// String path =
		// request.getSession().getServletContext().getRealPath("/")+"upload/jy";
		return null;
	}

	/**
	 * 
	 * allDelete:(删除配件). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = { "allDelete.do" }, produces = "application/jso;charset=utf-8")
	@ResponseBody
	public String allDelete(HttpServletRequest request, HttpServletResponse response, Map<String, Object> param) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Enumeration<String> enumeration = request.getParameterNames();
			String value = "";
			String name = "";
			List<String> list = new ArrayList<String>();
			while (enumeration.hasMoreElements()) {
				name = enumeration.nextElement();
				value = request.getParameter(name);
				list.add(value);
			}
			int row = jyItemPjServiceImpl.allDeleteById(list);
			if (row == 0) {
				map.put("resMsg", "删除失败！");
				map.put("flag", "1");
			} else {
				map.put("resMsg", "删除成功！");
				map.put("flag", "0");
			}
		} catch (Exception e) {
			map.put("resMsg", "删除失败！");
			map.put("flag", "1");
			JSONObject jsonObject = JSONObject.fromObject(map);
			return jsonObject.toString();
		}
		JSONObject jsonObject = JSONObject.fromObject(map);
		return jsonObject.toString();
	}

	/**
	 * 
	 * getPjLbList:(获取配件类别). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param param
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/pjlbList.do")
	public String getPjLbList(HttpServletRequest request, HttpServletResponse response, Map<String, Object> param) {
		Enumeration<String> enumeration = request.getParameterNames();
		String value = "";
		String name = "";
		Map<String, Object> map = new HashMap<String, Object>();
		while (enumeration.hasMoreElements()) {
			name = enumeration.nextElement();
			value = request.getParameter(name);
			map.put(name, value);
		}
		BasicUtil.startPage();
		List<JyItemPjlb> pjlblist = jyItemPjlbServiceImpl.getPJLB1(map);
		BasicUtil.endPage(pjlblist);
		request.setAttribute("pjlblist", pjlblist);
		request.setAttribute("pageNo",
				CommonUtils.isBlank(request.getParameter("pageNo")) ? 1 : request.getParameter("pageNo"));
		return "/WEB-INF/manager/mauto/jy/lb";
	}

	/**
	 * 
	 * savelb:(新增配件类别). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param param
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/savelb.do", produces = "application/jso;charset=utf-8")
	@ResponseBody
	public String savelb(HttpServletRequest request, HttpServletResponse response, Map<String, Object> param) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			ManagerSessionEntity managerSession = (ManagerSessionEntity) getSession(request,
					SessionConstEnum.MANAGER_SESSION);
			Enumeration<String> enumeration = request.getParameterNames();
			String value = "";
			String name = "";
			Map<String, Object> map = new HashMap<String, Object>();
			while (enumeration.hasMoreElements()) {
				name = enumeration.nextElement();
				value = request.getParameter(name);
				map.put(name, value);
			}
			map.put("createBy", managerSession.getManagerId());
			jyItemPjlbServiceImpl.insertPPLB(map);

			resultMap.put("flag", 0);
			resultMap.put("resMsg", "插入成功");
		} catch (Exception e) {
			resultMap.put("flag", 1);
			resultMap.put("resMsg", "插入失败！");
		}
		return JSONObject.fromObject(resultMap).toString();
	}

	/**
	 * 
	 * allDeleteLb:(删除配件类别). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param param
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = { "allDeleteLb.do" }, produces = "application/jso;charset=utf-8")
	@ResponseBody
	public String allDeleteLb(HttpServletRequest request, HttpServletResponse response, Map<String, Object> param) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Enumeration<String> enumeration = request.getParameterNames();
			String value = "";
			String name = "";
			List<String> list = new ArrayList<String>();
			while (enumeration.hasMoreElements()) {
				name = enumeration.nextElement();
				value = request.getParameter(name);
				list.add(value);
			}
			int row = jyItemPjlbServiceImpl.updatePJLB(list);
			if (row == 0) {
				map.put("resMsg", "删除失败！");
				map.put("flag", "1");
			} else {
				map.put("resMsg", "删除成功！");
				map.put("flag", "0");
			}
		} catch (Exception e) {
			map.put("resMsg", "删除失败！");
			map.put("flag", "1");
		}
		return JSONObject.fromObject(map).toString();
	}

	/**
	 * 
	 * getPjppList:(获取配件品牌). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param param
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/pjppList.do")
	public String getPjppList(HttpServletRequest request, HttpServletResponse response, Map<String, Object> param) {
		List<JyItemPjlb> pJlist = jyItemPjlbServiceImpl.getPJLB();
		Enumeration<String> enumeration = request.getParameterNames();
		String value = "";
		String name = "";
		Map<String, Object> map = new HashMap<String, Object>();
		while (enumeration.hasMoreElements()) {
			name = enumeration.nextElement();
			value = request.getParameter(name);
			map.put(name, value);
		}
		BasicUtil.startPage();
		List<JyItempjpp> pjpplist = jyItempjppServiceImpl.getPPXX(map);
		BasicUtil.endPage(pjpplist);
		request.setAttribute("pjpplist", pjpplist);
		request.setAttribute("pJlist", pJlist);
		request.setAttribute("pageNo",
				CommonUtils.isBlank(request.getParameter("pageNo")) ? 1 : request.getParameter("pageNo"));
		return "/WEB-INF/manager/mauto/jy/pp";
	}

	/**
	 * 
	 * ppsave:(保存配件品牌). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param param
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "ppsave.do", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String ppsave(HttpServletRequest request, HttpServletResponse response, Map<String, Object> param) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			ManagerSessionEntity managerSession = (ManagerSessionEntity) getSession(request,
					SessionConstEnum.MANAGER_SESSION);
			Enumeration<String> enumeration = request.getParameterNames();
			String value = "";
			String name = "";
			Map<String, Object> map = new HashMap<String, Object>();
			while (enumeration.hasMoreElements()) {
				name = enumeration.nextElement();
				value = request.getParameter(name);
				map.put(name, value);
			}
			map.put("createBy", managerSession.getManagerId());
			jyItempjppServiceImpl.insertSelective2(map);
			resultMap.put("flag", 0);
			resultMap.put("resMsg", "插入成功");
		} catch (Exception e) {
			resultMap.put("flag", 1);
			resultMap.put("resMsg", "插入失败！");
		}
		return JSONObject.fromObject(resultMap).toString();

	}

	/**
	 * 
	 * allDeletePP:(删除配件品牌信息). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param param
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/allDeletePP.do", produces = "application/jso;charset=utf-8")
	@ResponseBody
	public String allDeletePP(HttpServletRequest request, HttpServletResponse response, Map<String, Object> param) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Enumeration<String> enumeration = request.getParameterNames();
			String value = "";
			String name = "";
			List<String> list = new ArrayList<String>();
			while (enumeration.hasMoreElements()) {
				name = enumeration.nextElement();
				value = request.getParameter(name);
				list.add(value);
			}
			int row = jyItempjppServiceImpl.updatePJPP(list);
			if (row == 0) {
				map.put("resMsg", "删除失败！");
				map.put("flag", "1");
			} else {
				map.put("resMsg", "删除成功！");
				map.put("flag", "0");
			}
		} catch (Exception e) {
			map.put("resMsg", "删除失败！");
			map.put("flag", "1");
		}
		return JSONObject.fromObject(map).toString();
	}
}
